package myclient;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
/**
 * @author JIAO
 */
public class MainFrame implements ActionListener{

	/**
	 * HashMap用来存放TalkFrame窗口
	 * */
	public HashMap<String,TalkFrame> talkMap = new HashMap<String,TalkFrame>();
	
	/**
	 * 连接服务器的客户端  发送和接收消息
	 * */
	//建立连接的类
	public Connection conn;
	public JPanel panel;
	//好友树
	public JTree tree;
	//好友树的model
	public DefaultTreeModel model;
	//好友树的根节点
	private DefaultMutableTreeNode node;

	private JFrame fm;

	//我的账号
	public String myID;

	//根节点的图标
//	ImageIcon root_icon = null;
	
//	URL URL_root_icon1 = MainFrame.class.getResource("/image\\root1.jpg");
//	ImageIcon root_icon1 = new ImageIcon(URL_root_icon1);
//	URL URL_root_icon2 = MainFrame.class.getResource("/image\\root2.jpg");
//	ImageIcon root_icon2 = new ImageIcon(URL_root_icon2);

	//右键跳出的菜单
	public JPopupMenu popupMenu;
	
	public Timer t = new Timer(500, this);//头像闪动的时间间隔
	int verticalAligment =0;
	//用来存储发来的信息
	public HashMap<String,String> haveMsgMap = new HashMap<String,String>();

	public ReaderThread readerThread;

	public SettingListener lb_listener;
	private ImageFactory factory = new ImageFactory();
	public String ip;
	MainFrame(String ip,String sID, String strReturnPort) {
		this.ip = ip;
		this.myID = sID;
//		System.out.println("sName:"+sID+"-->"+"strReturn:"+strReturnPort);
//		root_icon = root_icon1;
		fm = new JFrame();
		fm.setResizable(false);
		conn = new Connection(ip,strReturnPort);
		System.out.println("MainFrame 的构造");
		fm.getContentPane().setBackground(new Color(0,200,255));
		fm.getContentPane().setLayout(null);
		fm.setBounds(100, 100, 230, 595);
		//窗口关闭监听器，当窗口关闭时，既下线，向服务器发送零。并关闭input output socket frame
		fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		panel.setBackground(Color.WHITE);
		JScrollPane scrollPane = new JScrollPane(panel);
		scrollPane.setBounds(34, 135, 165, 370);
		fm.getContentPane().add(scrollPane);
		
		int num = conn.list.size();
		panel.setSize(150, 360);
		
		
		node = new DefaultMutableTreeNode("我的好友");
		Iterator<String> list = conn.list.keySet().iterator();
		for(int i=0;i<num;i++){
			String id = list.next();
			node.add(new DefaultMutableTreeNode(id));
		}
		model = new DefaultTreeModel(node);
		tree = new JTree(model);
		MyTreeSelectionListener treeLis = new MyTreeSelectionListener(this);
		tree.addTreeSelectionListener(treeLis);
		tree.addMouseListener(treeLis);
		panel.add(tree);
		
		//右键弹出菜单，及其添加事件监听
		popupMenu = new JPopupMenu();
		JMenuItem item_talk = new JMenuItem("发送即时消息");
		JMenuItem item_delete = new JMenuItem("删除好友");
		JMenuItem item_move = new JMenuItem("将好友移动到");
		JMenuItem item_information = new JMenuItem("查看资料");
		popupMenu.add(item_talk);
		popupMenu.add(item_delete);
		popupMenu.add(item_move);
		popupMenu.add(item_information);
		popupMenu.addMouseListener(treeLis);
		item_talk.addActionListener(treeLis);
		item_delete.addActionListener(treeLis);
		item_move.addActionListener(treeLis);
		item_information.addActionListener(treeLis);
		//qq tree中头像的显示
		tree.setCellRenderer(new TreeCellRenderer(){
			String pic1 = "-1.gif";
			String pic2 = "-2.gif";
			String face = "/face/";
			String sNull = "null";
			public Component getTreeCellRendererComponent(JTree tree,
					Object value, boolean selected, boolean expanded,
					boolean leaf, int row, boolean hasFocus) {
				String s = value.toString();
				String name="";
				String pic="";
				if(conn.list.containsKey(s)){
					name = conn.list.get(s).getName();
					if(conn.list.get(s).getStatus().equals("0")){
						pic = conn.list.get(s).getPic()+pic2;
					}else
						pic = conn.list.get(s).getPic()+pic1;
				}else{
					
					return new JLabel(s);
				}
				if(pic.charAt(0)=='n'){
					pic = pic.replaceAll(sNull, "1");
				}
				
				JLabel lb = new JLabel(name,factory.make(face+pic),2);
				if(haveMsgMap.containsKey(s)){
					lb.setVerticalAlignment(verticalAligment);
				}
				lb.setName(s);
				lb.setPreferredSize(new Dimension(100,40));
				if(selected){
					lb.setOpaque(true);
					lb.setBackground(new Color(0,200,255));
				}
				return lb;
			}
			
		});
	
		fm.setTitle(conn.name);
		fm.setVisible(true);

		JLabel set_Label = new JLabel("设置");
//		ImageIcon icon = factory.make("/face/1-1.gif");
//		set_Label.setIcon(icon);
		set_Label.setBounds(0, 527, 55, 34);
		set_Label.setName("set");
		fm.getContentPane().add(set_Label);

		JLabel search_Label = new JLabel("查找");
		search_Label.setName("search");
//		search_Label.setIcon(factory.make("/face/1-1.gif"));
		search_Label.setBounds(96, 540, 45, 20);
		fm.getContentPane().add(search_Label);
		lb_listener = new SettingListener(set_Label,search_Label,this);
		set_Label.addMouseListener(lb_listener);
		search_Label.addMouseListener(lb_listener);
		readerThread = new ReaderThread(conn.reader,talkMap,conn.list,this,node);
		readerThread.start();//独立线程读线程开始监听
	}

	public void flush(){
		int num = conn.list.size();
		node.removeAllChildren();
		Iterator<String> it = conn.list.keySet().iterator();
		for(int i=0;i<num;i++){
			String id = it.next();
			node.add(new DefaultMutableTreeNode(id));
		}
		model.reload();
		fm.repaint();
		fm.setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {
		if(verticalAligment==0)
			verticalAligment = 1;
		else
			verticalAligment = 0;
		model.reload();
	}
}
